#include <stdint.h>
#include <string>
#include <iostream>
#include <sstream>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <md5c.h>
#include <stdio.h>
#include <sys/time.h>
#include <vector>

#include "app_platform/cntl_info.h"
#include "app_platform/cgi_stub_cntl.h"
#include "c2cplatform/library/the3/itil/c2c_attr.h"
#include "library/algorithm/algorithm.h"
#include "b2b2c/library/tmem_client_new.h"

#include "b2b2c_define.h"
#include "b2b2c/cooperator/ao/cooperatorao_stub4web_allinone.h"
#include "b2b2c/cooperator/po/cooperatorsubaccountpo_cooperator.h"
#include "b2b2c/cooperator/po/cooperatorpropo_cooperator.h"
#include "b2b2c/cooperator/po/cooperatorbaseinfopo_cooperator.h"	
#include "b2b2c/cooperator/po/cooperatorlicensepo_cooperator.h"
#include "b2b2c/cooperator/po/cooperatormonitormodelpo_cooperator.h"

using namespace b2b2c::comm;
using namespace b2b2c::comm::tmem;
using namespace b2b2c::cooperator::ao;

#define COOPERATOR_CMEM_PREFIX "Cooperator_"
#define COOPERATOR_TMEMCACHE_DEF_EXPIRE_TIME  90*3600*24
#define COOPERATOR_LOW_PROPERTY  "0000000000000000000000000000000000000000000000000000000000000000"

int32_t InitTMEM(uint32_t dwTmemBid){
    int32_t iRetCode = 0;
    std::string strTmemSvrName ="Cooperator_Tmem_Acc";
    std::map<TmemCacheItilEnum, uint32_t> mapTmemItil;
    uint64_t dwTmemExpireTime = COOPERATOR_TMEMCACHE_DEF_EXPIRE_TIME;
    uint32_t dwTmemReadSlowTime = TMEMCACHE_DEF_SLOW_TIME;
    uint32_t dwTmemBatchReadSlowTime = TMEMCACHE_DEF_SLOW_TIME;
    uint32_t dwTmemWriteSlowTime = TMEMCACHE_DEF_SLOW_TIME;
    uint32_t dwTmemBufferSize = TMEMCACHE_BUFFER_SIZE;
    uint32_t dwTmemUpdateAddrThreshold = TMEMCACHE_UPDATEADDR_THREDHOLD;
    bool bTmemUserNoKey = true;
    bool bTmemClosedCache = false;
    uint32_t dwTmemReadTimeout = TMEMCACHE_READ_TIMEOUT;
    uint32_t dwTmemFailedFreezeTime = TMEMCACHE_FAILED_FREEZE_TIME;
    uint32_t dwTmemConnectTimeout = TMEMCACHE_CONNECT_TIMEOUT;
    bTmemUserNoKey = true;
    bTmemClosedCache = false;

    uint32_t dwTmemWriteSlowItil = 0;
    uint32_t dwTmemReadSlowItil = 0;
    uint32_t dwTmemBatchReadSlowItil = 0;
    uint32_t dwTmemWriteErrorItil = 0;
    uint32_t dwTmemReadErrorItil = 0;
    uint32_t dwTmemBatchReadErrorItil = 0;
    uint32_t dwTmemWriteRequestItil = 0;
    uint32_t dwTmemReadRequestItil = 0;
    uint32_t dwTmemBatchReadRequestItil = 0;
    uint32_t dwTmemBatchReadCellRequestItil = 0;
    uint32_t dwTmemReadHitItil = 0;
    uint32_t dwTmemBatchReadCellHitItil = 0;


    mapTmemItil.insert(std::map<TmemCacheItilEnum, uint32_t>::value_type(ITIL_WRITE_SLOW,  dwTmemWriteSlowItil));
    mapTmemItil.insert(std::map<TmemCacheItilEnum, uint32_t>::value_type(ITIL_READ_SLOW, dwTmemReadSlowItil));
    mapTmemItil.insert(std::map<TmemCacheItilEnum, uint32_t>::value_type(ITIL_BATCHREAD_SLOW, dwTmemBatchReadSlowItil));
    mapTmemItil.insert(std::map<TmemCacheItilEnum, uint32_t>::value_type(ITIL_WRITE_ERR,  dwTmemWriteErrorItil));
    mapTmemItil.insert(std::map<TmemCacheItilEnum, uint32_t>::value_type(ITIL_READ_ERR, dwTmemReadErrorItil));
    mapTmemItil.insert(std::map<TmemCacheItilEnum, uint32_t>::value_type(ITIL_BATCHREAD_ERR, dwTmemBatchReadErrorItil));
    mapTmemItil.insert(std::map<TmemCacheItilEnum, uint32_t>::value_type(ITIL_WRITE_REQ,  dwTmemWriteRequestItil));
    mapTmemItil.insert(std::map<TmemCacheItilEnum, uint32_t>::value_type(ITIL_READ_REQ, dwTmemReadRequestItil));
    mapTmemItil.insert(std::map<TmemCacheItilEnum, uint32_t>::value_type(ITIL_BATCHREAD_REQ, dwTmemBatchReadRequestItil));
    mapTmemItil.insert(std::map<TmemCacheItilEnum, uint32_t>::value_type(ITIL_BATCHREAD_CELL_REQ, dwTmemBatchReadCellRequestItil));
    mapTmemItil.insert(std::map<TmemCacheItilEnum, uint32_t>::value_type(ITIL_READ_HIT, dwTmemReadHitItil));
    mapTmemItil.insert(std::map<TmemCacheItilEnum, uint32_t>::value_type(ITIL_BATCHREAD_CELL_HIT, dwTmemBatchReadCellHitItil));

    iRetCode = TMEM_CACHE_OP->Initialize(
            strTmemSvrName,
            dwTmemBid,
            mapTmemItil,
            dwTmemExpireTime,
            (uint16_t)dwTmemReadSlowTime,
            (uint16_t)dwTmemBatchReadSlowTime,
            (uint16_t)dwTmemWriteSlowTime,
            dwTmemBufferSize,
            dwTmemUpdateAddrThreshold,
            bTmemUserNoKey,
            bTmemClosedCache,
            dwTmemReadTimeout,
            dwTmemFailedFreezeTime,
            dwTmemConnectTimeout
    );

    if (0 != iRetCode)
    {
        std::cout << "TMEM_CACHE_OP Initialize() Failed!!" << std::endl;
        return -1;
    }
    else
    {
        std::cout << "TMEM_CACHE_OP Initialize Succ!" << std::endl;
    }
    return 0;
}

void Init()
{
	if(CCGI_STUB_CNTL->Initialize(true) != 0)
		std::cerr << "CCGI_STUB_CNTL->Init failed" << std::endl;
	CCGI_STUB_CNTL->setTimeout(10);
}

void TestUpdateCooperatorBaseInfo(uint32_t dwCooperatorId, uint8_t cState, std::string &strDiminutive,
                            std::string &strCooperatorAddrDiminutive, std::string &strCustomerHotLine)
{
	CCntlInfo oCntlInfo;
	oCntlInfo.setOperatorUin(dwCooperatorId);
	oCntlInfo.setOperatorClientIP(0);
	oCntlInfo.setOperatorKey("stw3ymf9uk");

	oCntlInfo.setRouteKey(dwCooperatorId);
	std::string machineKey = __FILE__;
	std::string source = __FILE__;
	uint32_t sceneId = 0;
	b2b2c::cooperator::po::CCooperatorBaseInfoPo cooperatorBaseInfoPo;
	cooperatorBaseInfoPo.SetCooperatorId(dwCooperatorId);
	cooperatorBaseInfoPo.SetCooperatorState(cState);
	cooperatorBaseInfoPo.SetCooperatorDiminutive(strDiminutive);
	cooperatorBaseInfoPo.SetCooperatorAddrDiminutive(strCooperatorAddrDiminutive);
	cooperatorBaseInfoPo.SetCustomerHotLine(strCustomerHotLine);
	std::string inReserve = "";		
	std::string errmsg = "";		
	std::string outReserve = "";		
	CCooperatorAoStub4Web oStub;

    uint32_t dwResult = oStub.UpdateCooperatorBaseInfo(
		oCntlInfo,
		machineKey,
		source,
		sceneId,
		cooperatorBaseInfoPo,
		inReserve,
		errmsg,
		outReserve
  	);
    if(dwResult == 0)
	{
		std::cout << "Invoke OK!" << std::endl;
		printf("OK result[0x%x], time = %ld\n"
        , dwResult, oStub.GetLastConsumeTime());
	}
	else
    {
        printf("Error result[0x%x], time = %ld , errmsg = [%s]\n"
        , dwResult, oStub.GetLastConsumeTime(),oStub.GetLastErrMsg());
    }
}

void TestGetCooperatorLicense()
{
	//TODO 请设置用户Passport
	CCntlInfo oCntlInfo;
	oCntlInfo.setOperatorUin(88883687);
	oCntlInfo.setOperatorClientIP(0);
	oCntlInfo.setOperatorKey("stw3ymf9uk");
	//TODO 请设置路由值
	oCntlInfo.setRouteKey(88883687);
	std::string machineKey = "test_client";
	std::string source = __FILE__;
	uint32_t sceneId = 0;
	uint64_t cooperatorId = 0;
	std::set<uint8_t > licenseType;
	std::string inReserve = "";		
	std::string errmsg = "";		
	std::vector<b2b2c::cooperator::po::CCooperatorLicensePo > cooperatorLicenseDdo;
	std::string outReserve = "";		
	CCooperatorAoStub4Web oStub;
	// TODO:替换函数参数
    uint32_t dwResult = oStub.GetCooperatorLicense(
		oCntlInfo,
		machineKey,
		source,
		sceneId,
		cooperatorId,
		licenseType,
		inReserve,
		errmsg,
		cooperatorLicenseDdo,
		outReserve
  	);
    if(dwResult == 0)
	{
		std::cout << "Invoke OK!" << std::endl;
		printf("OK result[0x%x], time = %ld\n"
        , dwResult, oStub.GetLastConsumeTime());
	}
	else
    {
        printf("Error result[0x%x], time = %ld , errmsg = [%s]\n"
        , dwResult, oStub.GetLastConsumeTime(),oStub.GetLastErrMsg());
    }
}
void TestGetCooperatorBaseInfo(uint64_t ddwCooperatorId)
{
	//TODO 如果使用配置中心来获取APP地址,请注释下行
	//CCGI_STUB_CNTL->setPeerAddr("172.25.34.123", 53101);

	//TODO 请设置用户Passport
    uint32_t i;
	CCntlInfo oCntlInfo;
	oCntlInfo.setOperatorUin(88883687);
	oCntlInfo.setOperatorClientIP(0);
	oCntlInfo.setOperatorKey("stw3ymf9uk");
	//TODO 请设置路由值
	oCntlInfo.setRouteKey(88883687);
    std::string machineKey = "test_client";
    std::string source = __FILE__;
	uint32_t sceneId = 0;
	uint64_t cooperatorId = ddwCooperatorId;
	std::string inReserve = "";		
	std::string errmsg = "";		
	b2b2c::cooperator::po::CCooperatorBaseInfoPo cooperatorBaseInfoPo;
	std::string outReserve = "";		
	CCooperatorAoStub4Web oStub;
	// TODO:替换函数参数
    uint32_t dwResult = oStub.GetCooperatorBaseInfo(
		oCntlInfo,
		machineKey,
		source,
		sceneId,
		cooperatorId,
		inReserve,
		errmsg,
		cooperatorBaseInfoPo,
		outReserve
  	);
    if(dwResult == 0)
	{
		std::cout << "Invoke OK!" << std::endl;
		printf("OK result[0x%x], time = %ld\n"
        , dwResult, oStub.GetLastConsumeTime());

		std::cout<<"CooperatorId:"<< cooperatorBaseInfoPo.GetCooperatorId() << std::endl;
		std::cout<<"CooperatorType:"<< (cooperatorBaseInfoPo.GetCooperatorType()+0) << std::endl;
        std::cout<<"CooperatorState:"<< (cooperatorBaseInfoPo.GetCooperatorState()+0)<< std::endl;
        std::cout<<"CooperatorSpid:"<< cooperatorBaseInfoPo.GetCooperatorSpid() << std::endl;
        std::cout<<"CooperatorProperty:"<< cooperatorBaseInfoPo.GetCooperatorProperty()<< std::endl;
        std::cout<<"CooperatorPhone:"<< cooperatorBaseInfoPo.GetCooperatorPhone()<< std::endl;
        std::cout<<"CooperatorName:"<< cooperatorBaseInfoPo.GetCooperatorName() << std::endl;
        std::cout<<"CooperatorLegalPerson:"<< cooperatorBaseInfoPo.GetCooperatorLegalPerson()<< std::endl;
        std::cout<<"CooperatorLastUpdateTime:"<< cooperatorBaseInfoPo.GetCooperatorLastUpdateTime()<< std::endl;
        std::cout<<"CooperatorFax:"<< cooperatorBaseInfoPo.GetCooperatorFax()<< std::endl;
        std::cout<<"CooperatorEmail:"<< cooperatorBaseInfoPo.GetCooperatorEmail()<< std::endl;
        std::cout<<"CooperatorAddress:"<< cooperatorBaseInfoPo.GetCooperatorAddress()<< std::endl;
        std::cout<<"CooperatorAddTime:"<< cooperatorBaseInfoPo.GetCooperatorAddTime()<< std::endl;
        std::cout<<"cooperatordiminutive:"<< cooperatorBaseInfoPo.GetCooperatorDiminutive()<< std::endl;
        std::cout<<"CooperatorAddrDiminutive:"<< cooperatorBaseInfoPo.GetCooperatorAddrDiminutive()<< std::endl;
        std::cout<<"CustomerHotLine:"<< cooperatorBaseInfoPo.GetCustomerHotLine()<< std::endl;
        std::cout<<"=======================================================" << std::endl;

        for(i=0; i<cooperatorBaseInfoPo.GetCooperatorSubAccountPo().size(); i++)
        {
            std::cout<<"CooperatorSubAccountId:"<< cooperatorBaseInfoPo.GetCooperatorSubAccountPo()[i].GetCooperatorSubAccountId()<< std::endl;
            std::cout<<"GetCooperatorId:"<< cooperatorBaseInfoPo.GetCooperatorSubAccountPo()[i].GetCooperatorId()<< std::endl;
            std::cout<<"GetNavId:"<< cooperatorBaseInfoPo.GetCooperatorSubAccountPo()[i].GetNavId()<< std::endl;
            std::cout<<"GetNavName:"<< cooperatorBaseInfoPo.GetCooperatorSubAccountPo()[i].GetNavName()<< std::endl;
            std::cout<<"GetCooperatorSubAccountState:"<< cooperatorBaseInfoPo.GetCooperatorSubAccountPo()[i].GetCooperatorSubAccountState()<< std::endl;
            std::cout<<"GetCooperatorProtocolId:"<< cooperatorBaseInfoPo.GetCooperatorSubAccountPo()[i].GetCooperatorProtocolId()<< std::endl;
            std::cout<<"CooperatorSubAccountProperty:"<< cooperatorBaseInfoPo.GetCooperatorSubAccountPo()[i].GetCooperatorSubAccountProperty()<< std::endl;
            std::cout<<"GetCooperatorSubAccountAddTime:"<< cooperatorBaseInfoPo.GetCooperatorSubAccountPo()[i].GetCooperatorSubAccountAddTime()<< std::endl;
            std::cout<<"GetCooperatorSubAccountLastUpdateTime:"<< cooperatorBaseInfoPo.GetCooperatorSubAccountPo()[i].GetCooperatorSubAccountLastUpdateTime()<< std::endl;
        }
	}
	else
    {
        printf("Error result[0x%x], time = %ld , errmsg = [%s]\n"
        , dwResult, oStub.GetLastConsumeTime(),oStub.GetLastErrMsg());
    }
}
void TestSetCooperatorPassWd()
{
	//TODO 如果使用配置中心来获取APP地址,请注释下行

	//TODO 请设置用户Passport
	CCntlInfo oCntlInfo;
	oCntlInfo.setOperatorUin(88883687);
	oCntlInfo.setOperatorClientIP(0);
	oCntlInfo.setOperatorKey("stw3ymf9uk");
	//TODO 请设置路由值
	oCntlInfo.setRouteKey(88883687);
    std::string machineKey = "test_client";
    std::string source = __FILE__;
	uint32_t sceneId = 0;
	uint64_t cooperatorId = 0;
	std::string cooperatorOldPassword = "";		
	std::string cooperatorNewPassword = "";		
	std::string inReserve = "";		
	std::string errmsg = "";		
	std::string outReserve = "";		
	CCooperatorAoStub4Web oStub;
	// TODO:替换函数参数
    uint32_t dwResult = oStub.SetCooperatorPassWd(
		oCntlInfo,
		machineKey,
		source,
		sceneId,
		cooperatorId,
		cooperatorOldPassword,
		cooperatorNewPassword,
		inReserve,
		errmsg,
		outReserve
  	);
    if(dwResult == 0)
	{
		std::cout << "Invoke OK!" << std::endl;
		printf("OK result[0x%x], time = %ld\n"
        , dwResult, oStub.GetLastConsumeTime());
	}
	else
    {
        printf("Error result[0x%x], time = %ld , errmsg = [%s]\n"
        , dwResult, oStub.GetLastConsumeTime(),oStub.GetLastErrMsg());
    }
}
void TestCheckCooperatorPassWd()
{
	//TODO 如果使用配置中心来获取APP地址,请注释下行
	CCGI_STUB_CNTL->setPeerAddr("172.25.34.123", 53101);

	//TODO 请设置用户Passport
	CCntlInfo oCntlInfo;
	oCntlInfo.setOperatorUin(88883687);
	oCntlInfo.setOperatorClientIP(0);
	oCntlInfo.setOperatorKey("stw3ymf9uk");
	//TODO 请设置路由值
	oCntlInfo.setRouteKey(88883687);
    std::string machineKey = "test_client";
    std::string source = __FILE__;
	uint32_t sceneId = 0;
	uint64_t cooperatorId = 0;
	std::string cooperatorPassword = "";		
	std::string inReserve = "";		
	std::string errmsg = "";		
	std::string outReserve = "";		
	CCooperatorAoStub4Web oStub;
	// TODO:替换函数参数
    uint32_t dwResult = oStub.CheckCooperatorPassWd(
		oCntlInfo,
		machineKey,
		source,
		sceneId,
		cooperatorId,
		cooperatorPassword,
		inReserve,
		errmsg,
		outReserve
  	);
    if(dwResult == 0)
	{
		std::cout << "Invoke OK!" << std::endl;
		printf("OK result[0x%x], time = %ld\n"
        , dwResult, oStub.GetLastConsumeTime());
	}
	else
    {
        printf("Error result[0x%x], time = %ld , errmsg = [%s]\n"
        , dwResult, oStub.GetLastConsumeTime(),oStub.GetLastErrMsg());
    }
}

void TestUpdateCooperatorLicense()
{
	//TODO 如果使用配置中心来获取APP地址,请注释下行
	CCGI_STUB_CNTL->setPeerAddr("172.25.34.123", 53101);

	//TODO 请设置用户Passport
	CCntlInfo oCntlInfo;
	oCntlInfo.setOperatorUin(88883687);
	oCntlInfo.setOperatorClientIP(0);
	oCntlInfo.setOperatorKey("stw3ymf9uk");
	//TODO 请设置路由值
	oCntlInfo.setRouteKey(88883687);
    std::string machineKey = "test_client";
    std::string source = __FILE__;
	uint32_t sceneId = 0;
	b2b2c::cooperator::po::CCooperatorLicensePo cooperatorLicensePo;
	std::string inReserve = "";		
	std::string errmsg = "";		
	std::string outReserve = "";		
	CCooperatorAoStub4Web oStub;
	// TODO:替换函数参数
    uint32_t dwResult = oStub.UpdateCooperatorLicense(
		oCntlInfo,
		machineKey,
		source,
		sceneId,
		cooperatorLicensePo,
		inReserve,
		errmsg,
		outReserve
  	);
    if(dwResult == 0)
	{
		std::cout << "Invoke OK!" << std::endl;
		printf("OK result[0x%x], time = %ld\n"
        , dwResult, oStub.GetLastConsumeTime());
	}
	else
    {
        printf("Error result[0x%x], time = %ld , errmsg = [%s]\n"
        , dwResult, oStub.GetLastConsumeTime(),oStub.GetLastErrMsg());
    }
}

void TestGetCooperatorBaseInfoList(uint64_t ddwCooperatorId)
{
    //TODO 如果使用配置中心来获取APP地址,请注释下行
    //TODO 请设置用户Passport
    CCntlInfo oCntlInfo;
    oCntlInfo.setOperatorUin(88883687);
    oCntlInfo.setOperatorClientIP(0);
    oCntlInfo.setOperatorKey("stw3ymf9uk");
    //TODO 请设置路由值
    oCntlInfo.setRouteKey(88883687);
    std::string machineKey = "test_client";
    std::string source = __FILE__;
    uint32_t sceneId = 0;
    std::set<uint64_t > cooperatorId;
    cooperatorId.insert(855006089);
    cooperatorId.insert(855006090);
    std::string inReserve = "";
    std::string errmsg = "";
    std::vector<b2b2c::cooperator::po::CCooperatorBaseInfoPo > cooperatorBaseInfoPo;
    std::string outReserve = "";
    CCooperatorAoStub4Web oStub;
    // TODO:替换函数参数
    uint32_t dwResult = oStub.GetCooperatorBaseInfoList(
        oCntlInfo,
        machineKey,
        source,
        sceneId,
        cooperatorId,
        inReserve,
        errmsg,
        cooperatorBaseInfoPo,
        outReserve
    );
    if(dwResult == 0)
    {
        std::cout << "Invoke OK!" << std::endl;
        printf("OK result[0x%x], time = %ld\n"
        , dwResult, oStub.GetLastConsumeTime());

        for(uint32_t i=0; i<cooperatorBaseInfoPo.size(); i++)
        {
            std::cout<<"CooperatorId:"<< cooperatorBaseInfoPo[i].GetCooperatorId() << std::endl;
            std::cout<<"CooperatorType:"<< cooperatorBaseInfoPo[i].GetCooperatorType() << std::endl;
            std::cout<<"CooperatorState:"<< cooperatorBaseInfoPo[i].GetCooperatorState()<< std::endl;
            std::cout<<"CooperatorSpid:"<< cooperatorBaseInfoPo[i].GetCooperatorSpid() << std::endl;
            std::cout<<"CooperatorProperty:"<< cooperatorBaseInfoPo[i].GetCooperatorProperty()<< std::endl;
            std::cout<<"CooperatorPhone:"<< cooperatorBaseInfoPo[i].GetCooperatorPhone()<< std::endl;
            std::cout<<"CooperatorName:"<< cooperatorBaseInfoPo[i].GetCooperatorName() << std::endl;
            std::cout<<"CooperatorLegalPerson:"<< cooperatorBaseInfoPo[i].GetCooperatorLegalPerson()<< std::endl;
            std::cout<<"CooperatorLastUpdateTime:"<< cooperatorBaseInfoPo[i].GetCooperatorLastUpdateTime()<< std::endl;
            std::cout<<"CooperatorFax:"<< cooperatorBaseInfoPo[i].GetCooperatorFax()<< std::endl;
            std::cout<<"CooperatorEmail:"<< cooperatorBaseInfoPo[i].GetCooperatorEmail()<< std::endl;
            std::cout<<"CooperatorAddress:"<< cooperatorBaseInfoPo[i].GetCooperatorAddress()<< std::endl;
            std::cout<<"CooperatorAddTime:"<< cooperatorBaseInfoPo[i].GetCooperatorAddTime()<< std::endl;
            std::cout<<"CooperatorAddrDiminutive:"<< cooperatorBaseInfoPo[i].GetCooperatorAddrDiminutive()<< std::endl;
            std::cout<<"CustomerHotLine:"<< cooperatorBaseInfoPo[i].GetCustomerHotLine()<< std::endl;

            for(uint32_t j=0; j<cooperatorBaseInfoPo[i].GetCooperatorSubAccountPo().size(); j++)
            {
                std::cout<<"CooperatorSubAccountId:"<< cooperatorBaseInfoPo[i].GetCooperatorSubAccountPo()[j].GetCooperatorSubAccountId()<< std::endl;
                std::cout<<"GetCooperatorId:"<< cooperatorBaseInfoPo[i].GetCooperatorSubAccountPo()[j].GetCooperatorId()<< std::endl;
                std::cout<<"GetCooperatorSubAccountState:"<< cooperatorBaseInfoPo[i].GetCooperatorSubAccountPo()[j].GetCooperatorSubAccountState()<< std::endl;
                std::cout<<"GetCooperatorProtocolId:"<< cooperatorBaseInfoPo[i].GetCooperatorSubAccountPo()[j].GetCooperatorProtocolId()<< std::endl;
                std::cout<<"GetCooperatorSubAccountAddTime:"<< cooperatorBaseInfoPo[i].GetCooperatorSubAccountPo()[j].GetCooperatorSubAccountAddTime()<< std::endl;
                std::cout<<"GetCooperatorSubAccountLastUpdateTime:"<< cooperatorBaseInfoPo[i].GetCooperatorSubAccountPo()[j].GetCooperatorSubAccountLastUpdateTime()<< std::endl;
            }
        }
    }
    else
    {
        printf("Error result[0x%x], time = %ld , errmsg = [%s]\n"
        , dwResult, oStub.GetLastConsumeTime(),oStub.GetLastErrMsg());
    }
}

void TestDelCooperatorCache(uint32_t dwTmemBid, uint32_t dwCooperatorId)
{
    int32_t iRetCode = 0;

    //初始化TMEM
    iRetCode = InitTMEM(dwTmemBid);
    if(iRetCode != 0)
    {
        std::cout<<"InitTmem Initialize Failed!"<<std::endl;
        return;
    }

    std::stringstream ssKey;
    ssKey << COOPERATOR_CMEM_PREFIX;
    ssKey << dwCooperatorId;

    //删除TMEM记录
    iRetCode = TMEM_CACHE_OP->DelData(ssKey.str());
    if(iRetCode)
    {
        std::cout << "TMEM_CACHE_OP->DelData Failed, sKey:" << ssKey.str() << " ErrCode:" << iRetCode << std::endl;
    }else{
        std::cout << "TMEM_CACHE_OP->DelData Sucess, sKey:" << ssKey.str() << " ErrCode:" << iRetCode << std::endl;
    }
}

//dev下使用默认bid(20120335)
void TestDelMonitorModelCache(uint32_t dwTmemBid, uint32_t dwCooperatorId)
{
    int32_t iRetCode = 0;

    //初始化TMEM
    iRetCode = InitTMEM(dwTmemBid);
    if(iRetCode != 0)
    {
        std::cout<<"InitTmem Initialize Failed!"<<std::endl;
        return;
    }

    std::stringstream ssKey;
    ssKey << "CooperatorMonitor_";
    ssKey << dwCooperatorId;

    //删除TMEM记录
    iRetCode = TMEM_CACHE_OP->DelData(ssKey.str());
    if(iRetCode)
    {
        std::cout << "TMEM_CACHE_OP->DelData Failed, sKey:" << ssKey.str() << " ErrCode:" << iRetCode << std::endl;
    }else{
        std::cout << "TMEM_CACHE_OP->DelData Sucess, sKey:" << ssKey.str() << " ErrCode:" << iRetCode << std::endl;
    }
}

void TestLogisticsSetCooperatorPro(uint32_t dwCooperatorId, std::string& strLogisticsProIn, std::string& strLogisticsProEx)
{
    CCntlInfo oCntlInfo;
    oCntlInfo.setOperatorUin(dwCooperatorId);
    oCntlInfo.setOperatorClientIP(0);
    oCntlInfo.setOperatorKey("stw3ymf9uk");

    oCntlInfo.setRouteKey(dwCooperatorId);
    std::string machineKey = __FILE__;
    std::string source = __FILE__;
    uint32_t sceneId = 0;
    b2b2c::cooperator::po::CCooperatorProPo oCooperatorProPo;
    oCooperatorProPo.SetCooperatorId(dwCooperatorId);
    std::bitset<128> bsInclude(strLogisticsProIn+COOPERATOR_LOW_PROPERTY);
    oCooperatorProPo.SetBitInclude(bsInclude);
    std::bitset<128> bsExclude(strLogisticsProEx+COOPERATOR_LOW_PROPERTY);
    oCooperatorProPo.SetBitExclude(bsExclude);

    std::string inReserve = "";
    std::string errmsg = "";
    std::string outReserve = "";
    CCooperatorAoStub4Web oStub;
    b2b2c::cooperator::po::CCooperatorBaseInfoPo cooperatorBaseInfoPo;
    uint32_t dwResult = oStub.LogisticsSetCooperatorPro(
        oCntlInfo,
        machineKey,
        source,
        sceneId,
        0,
        oCooperatorProPo,
        inReserve,
        errmsg,
        cooperatorBaseInfoPo,
        outReserve
    );
    if(dwResult == 0)
    {
        std::cout << "Invoke OK!" << std::endl;
        printf("OK result[0x%x], time = %ld\n"
        , dwResult, oStub.GetLastConsumeTime());
        std::cout << "ResCooperatorPro:" << cooperatorBaseInfoPo.GetCooperatorProperty() << std::endl;
    }
    else
    {
        printf("Error result[0x%x], time = %ld , errmsg = [%s]\n"
        , dwResult, oStub.GetLastConsumeTime(),oStub.GetLastErrMsg());
    }
}

void TestGetCooperatorMonitorModelList(uint32_t dwCooperatorId)
{
	//TODO 如果使用配置中心来获取APP地址,请注释下行
	CCGI_STUB_CNTL->setPeerAddr("10.6.223.128", 53101);

	CCntlInfo oCntlInfo;
	oCntlInfo.setOperatorUin(dwCooperatorId);
	oCntlInfo.setOperatorClientIP(0);
	oCntlInfo.setOperatorKey("stw3ymf9uk");

	oCntlInfo.setRouteKey(dwCooperatorId);
	std::string machineKey = __FILE__;
	std::string source = __FILE__;
	uint32_t sceneId = 0;


	std::string inReserve = "";
	std::string errmsg = "";
	std::string outReserve = "";
	CCooperatorAoStub4Web oStub;

	std::vector<b2b2c::cooperator::po::CCooperatorMonitorModelPo> vecCMModelPo;
	std::string strRulesruleJson;

	uint32_t dwResult = oStub.GetCooperatorMonitorModelList(
		oCntlInfo,
		machineKey,
		source,
		sceneId,
		dwCooperatorId,
		inReserve,
		errmsg,
		vecCMModelPo,
		strRulesruleJson,
		outReserve
	);

	if(dwResult == 0)
	{
		std::cout << "Invoke OK!" << std::endl;
		printf("OK result[0x%x], time = %ld\n", dwResult, oStub.GetLastConsumeTime());

		std::cout << "RulesruleJson:" << strRulesruleJson << std::endl;
		std::cout << "vecCMModelPo:" << std::endl;
		for(uint32_t i=0; i<vecCMModelPo.size(); i++)
		{
			std::cout << "{";
			std::cout<<"ModelId:"<< vecCMModelPo[i].GetModelId() << std::endl;
			std::cout<<"CooperatorId:"<< vecCMModelPo[i].GetCooperatorId()<< std::endl;
			std::cout<<"ModelName:"<< vecCMModelPo[i].GetModelName()<< std::endl;
			std::cout<<"ModelState:"<< vecCMModelPo[i].GetModelState() << std::endl;
			std::cout<<"MonitorTouch:"<< vecCMModelPo[i].GetMonitorTouch() << std::endl;
			std::cout<<"ModelType:"<< vecCMModelPo[i].GetModelType() << std::endl;
			std::cout<<"AddTime:"<< vecCMModelPo[i].GetModelAddTime() << std::endl;
			std::cout<<"LastModifyTime:"<< vecCMModelPo[i].GetModelLastUpdateTime() << std::endl;
			std::cout<<"ModelRule:[";
			std::vector<std::vector<b2b2c::cooperator::po::CCooperatorMonitorModelRulePo> > vevVecCMMRulePo = vecCMModelPo[i].GetModelRule();
			for(uint32_t j=0; j<vevVecCMMRulePo.size(); j++)
			{
				std::cout<<"[";
				for(uint32_t k=0;k<vevVecCMMRulePo[j].size();k++)
				{
					std::cout<<"{";
					std::cout<<"mo:"<<vevVecCMMRulePo[j][k].GetMo()<<",";
					std::cout<<"co:"<<vevVecCMMRulePo[j][k].GetCo()<<",";
					std::cout<<"cm:"<<vevVecCMMRulePo[j][k].GetCm()<<",";
					std::cout<<"cv:"<<vevVecCMMRulePo[j][k].GetCv()<<"}";
				}
				std::cout<<"]"<< std::endl;
			}
			std::cout<<"]"<< std::endl;
			std::cout << "}" << std::endl;
		}
	}
	else
	{
		printf("Error result[0x%x], time = %ld , errmsg = [%s]\n"
		, dwResult, oStub.GetLastConsumeTime(),oStub.GetLastErrMsg());
	}
}

void TestUpdateCooperatorMonitorModel(
		uint32_t dwCooperatorId,
		uint16_t dwModelId_u,
		uint32_t dwModelId,
		uint16_t strModelName_u,
		std::string strModelName,
		uint16_t dwModelState_u,
		uint16_t dwModelState,
		uint16_t dwMonitorTouch_u,
		uint16_t dwMonitorTouch,
		uint16_t dwModelType_u,
		uint16_t dwModelType,
		uint16_t vecModelRule_u,
		uint16_t dwModelRuleSwitch)
{
	//TODO 如果使用配置中心来获取APP地址,请注释下行
	CCGI_STUB_CNTL->setPeerAddr("10.6.223.128", 53101);

	CCntlInfo oCntlInfo;
	oCntlInfo.setOperatorUin(dwCooperatorId);
	oCntlInfo.setOperatorClientIP(0);
	oCntlInfo.setOperatorKey("stw3ymf9uk");

	oCntlInfo.setRouteKey(dwCooperatorId);
	std::string machineKey = __FILE__;
	std::string source = __FILE__;
	uint32_t sceneId = 0;


	std::string inReserve = "";
	std::string errmsg = "";
	std::string outReserve = "";
	CCooperatorAoStub4Web oStub;

	b2b2c::cooperator::po::CCooperatorMonitorModelPo cMModelPo;
	cMModelPo.SetCooperatorId(dwCooperatorId);

	//构造po内容
	if(dwModelId_u)
	{
		cMModelPo.SetModelId(dwModelId);
	}

	//名称
	if(strModelName_u)
	{
		cMModelPo.SetModelName(strModelName);
	}

	//状态
	if(dwModelState_u)
	{
		cMModelPo.SetModelState(dwModelState);
	}

	//触达类型
	if(dwMonitorTouch_u)
	{
		cMModelPo.SetMonitorTouch(dwMonitorTouch);
	}

	//模版类型
	if(dwModelType_u)
	{
		cMModelPo.SetModelType(dwModelType);
	}


	if(vecModelRule_u)
	{
		std::vector<b2b2c::cooperator::po::CCooperatorMonitorModelRulePo> vecRulePo;
		b2b2c::cooperator::po::CCooperatorMonitorModelRulePo rulePo;
		switch(dwModelRuleSwitch)
		{
			case 1:
				rulePo.SetMo(1);
				rulePo.SetCo(3);
				rulePo.SetCm(5);
				rulePo.SetCv(50);
				break;
			case 2:
				rulePo.SetMo(1);
				rulePo.SetCo(0);
				rulePo.SetCm(2);
				rulePo.SetCv(100);
				break;
			default:
				rulePo.SetMo(1);
				rulePo.SetCo(3);
				rulePo.SetCm(4);
				rulePo.SetCv(50);
				break;
		}
		vecRulePo.push_back(rulePo);
		cMModelPo.GetModelRule().push_back(vecRulePo);
		cMModelPo.SetModelRule_u(1);
	}

	uint32_t dwResult = oStub.UpdateCooperatorMonitorModel(
		oCntlInfo,
		machineKey,
		source,
		sceneId,
		cMModelPo,
		inReserve,
		errmsg,
		outReserve
	);

	if(dwResult == 0)
	{
		std::cout << "Invoke OK!" << std::endl;
		printf("OK result[0x%x], time = %ld\n", dwResult, oStub.GetLastConsumeTime());
	}
	else
	{
		printf("Error result[0x%x], time = %ld , errmsg = [%s]\n"
		, dwResult, oStub.GetLastConsumeTime(),errmsg.c_str());
	}
}

void TestDeleteCooperatorMonitorModel(uint32_t dwCooperatorId,uint32_t dwModelId)
{
	//TODO 如果使用配置中心来获取APP地址,请注释下行
	CCGI_STUB_CNTL->setPeerAddr("10.6.223.128", 53101);

	CCntlInfo oCntlInfo;
	oCntlInfo.setOperatorUin(dwCooperatorId);
	oCntlInfo.setOperatorClientIP(0);
	oCntlInfo.setOperatorKey("stw3ymf9uk");

	oCntlInfo.setRouteKey(dwCooperatorId);
	std::string machineKey = __FILE__;
	std::string source = __FILE__;
	uint32_t sceneId = 0;


	std::string inReserve = "";
	std::string errmsg = "";
	std::string outReserve = "";
	CCooperatorAoStub4Web oStub;

	uint32_t dwResult = oStub.DeleteCooperatorMonitorModel(
		oCntlInfo,
		machineKey,
		source,
		sceneId,
		dwCooperatorId,
		dwModelId,
		inReserve,
		errmsg,
		outReserve
	);

	if(dwResult == 0)
	{
		std::cout << "Invoke OK!" << std::endl;
		printf("OK result[0x%x], time = %ld\n", dwResult, oStub.GetLastConsumeTime());
	}
	else
	{
		printf("Error result[0x%x], time = %ld , errmsg = [%s]\n"
		, dwResult, oStub.GetLastConsumeTime(),oStub.GetLastErrMsg());
	}
}

void TestGetCooperatorIdByIcsonId(uint64_t ddwisconId)
{
	 CCntlInfo oCntlInfo;
    oCntlInfo.setOperatorUin(88883687);
    oCntlInfo.setOperatorClientIP(0);
    oCntlInfo.setOperatorKey("stw3ymf9uk");
    //TODO 请设置路由值
    oCntlInfo.setRouteKey(88883687);
    std::string machineKey = "test_client";
    std::string source = __FILE__;
    uint32_t sceneId = 0;
    uint64_t icsonId;
	icsonId = ddwisconId;
    std::string inReserve = "";
    std::string errmsg = "";
    uint64_t cooperatorId = 0;
    std::string outReserve = "";
    CCooperatorAoStub4Web oStub;
    // TODO:替换函数参数
    uint32_t dwResult = oStub.GetCooperatorIdByIcsonId(
        oCntlInfo,
        machineKey,
        source,
        sceneId,
        icsonId,
        inReserve,
        errmsg,
        cooperatorId,
        outReserve
    );
    if(dwResult == 0)
    {
        std::cout << "Invoke OK!" << std::endl;
        printf("OK result[0x%x], time = %ld\n"
        , dwResult, oStub.GetLastConsumeTime());
		std::cout <<"Succeed,The icsonId:"<<icsonId<<" And CooperatorId: "<<cooperatorId<<std::endl;
	}
	else
    {
        printf("Error result[0x%x], time = %ld , errmsg = [%s]\n"
        , dwResult, oStub.GetLastConsumeTime(),oStub.GetLastErrMsg());
    }
}

//测试主函数
int main(int argc, char** argv)
{
    //测试主流程
    // 开发人员可以根据业务需要进行顺序的调整，以期达到更好的测试效果
    // 比如，可以按照插入、选择、修改、选择、删除、选择 顺序进行处理
    //
    if(argc >= 3)
    {
        Init();
        int n = atoi(argv[1]);
        uint64_t ddwCooperatorId = 0;
        uint32_t dwCooperatorId = 0;
        uint8_t cState = 0;

        switch(n)
        {
        case 1:
        {
            ddwCooperatorId = (uint64_t)(strtoul(argv[2], NULL, 10));
            printf("TestGetCooperatorBaseInfo()\n");
            TestGetCooperatorBaseInfo(ddwCooperatorId);
            break;
        }
        case 2:
        {
            ddwCooperatorId = (uint64_t)(strtoul(argv[2], NULL, 10));
            printf("TestGetCooperatorBaseInfoList()\n");
            TestGetCooperatorBaseInfoList(ddwCooperatorId);
            break;
        }
        case 3:
        {
            dwCooperatorId = (uint32_t)(strtoul(argv[2], NULL, 10));
            cState = (uint8_t)(strtoul(argv[3], NULL, 10));
            std::string strDiminutive(argv[4]);
            std::string strCooperatorAddrDiminutive(argv[5]);
            std::string strCustomerHotLine(argv[6]);
            printf("TestUpdateCooperatorBaseInfo()\n");
            TestUpdateCooperatorBaseInfo(dwCooperatorId, cState, strDiminutive, strCooperatorAddrDiminutive,
                    strCustomerHotLine);
            break;
        }
        case 4:
        {
            if(argc < 5){
                printf("No[4] TestLogisticsSetCooperatorPro() parm:dwCooperatorId strLogisticsProIn strLogisticsProEx\n");
                return 0;
            }
            dwCooperatorId = (uint32_t)(strtoul(argv[2], NULL, 10));
            std::string strLogisticsProIn(argv[3]);
            std::string strLogisticsProEx(argv[4]);
            TestLogisticsSetCooperatorPro(dwCooperatorId, strLogisticsProIn, strLogisticsProEx);
            break;
        }
        case 6:
        {
            uint32_t dwTmemBid = 0;
            dwTmemBid = (uint32_t)(strtoul(argv[2], NULL, 10));
            dwCooperatorId = (uint32_t)(strtoul(argv[3], NULL, 10));
            printf("TestDelCooperatorCache()\n");
            TestDelCooperatorCache(dwTmemBid, dwCooperatorId);
            break;
        }
        case 7:
		{
			dwCooperatorId = (uint32_t)(strtoul(argv[2], NULL, 10));
			printf("TestGetCooperatorMonitorModelList()\n");
			TestGetCooperatorMonitorModelList(dwCooperatorId);
			break;
		}
        case 8:
		{
			dwCooperatorId = (uint32_t)(strtoul(argv[2], NULL, 0));
			uint16_t dwModelId_u = (uint16_t)(strtoul(argv[3], NULL, 0));
			uint32_t dwModelId = (uint32_t)(strtoul(argv[4], NULL, 0));

			uint16_t strModelName_u = (uint16_t)(strtoul(argv[5], NULL, 0));
			std::string strModelName(argv[6]);

			uint16_t dwModelState_u = (uint16_t)(strtoul(argv[7], NULL, 0));
			uint16_t dwModelState = (uint16_t)(strtoul(argv[8], NULL, 0));

			uint16_t dwMonitorTouch_u = (uint16_t)(strtoul(argv[9], NULL, 0));
			uint16_t dwMonitorTouch = (uint16_t)(strtoul(argv[10], NULL, 0));

			uint16_t dwModelType_u = (uint16_t)(strtoul(argv[11], NULL, 0));
			uint16_t dwModelType = (uint16_t)(strtoul(argv[12], NULL, 0));

			uint16_t vecModelRule_u = (uint16_t)(strtoul(argv[13], NULL, 0));
			uint16_t dwModelRuleSwitch = (uint16_t)(strtoul(argv[14], NULL, 0));

			printf("TestUpdateCooperatorMonitorModel()\n");
			TestUpdateCooperatorMonitorModel(
					dwCooperatorId,
					dwModelId_u,
					dwModelId,
					strModelName_u,
					strModelName,
					dwModelState_u,
					dwModelState,
					dwMonitorTouch_u,
					dwMonitorTouch,
					dwModelType_u,
					dwModelType,
					vecModelRule_u,
					dwModelRuleSwitch);
			break;
		}
        case 9:
		{
			dwCooperatorId = (uint32_t)(strtoul(argv[2], NULL, 10));
			uint32_t dwModelId = (uint32_t)(strtoul(argv[3], NULL, 10));
			printf("TestDeleteCooperatorMonitorModel()\n");
			TestDeleteCooperatorMonitorModel(dwCooperatorId,dwModelId);
			break;
		}
        case 10:
        {
        	uint32_t dwTmemBid = 0;
			dwTmemBid = (uint32_t)(strtoul(argv[2], NULL, 10));
			dwCooperatorId = (uint32_t)(strtoul(argv[3], NULL, 10));
        	printf("TestDelMonitorModelCache()\n");
        	TestDelMonitorModelCache(dwTmemBid,dwCooperatorId);
        	break;
        }
		case 11:
		{
			uint64_t ddwicsonId = (uint64_t)(strtoul(argv[2], NULL, 10));
			printf("TestGetCooperatorIdByIcsonId()\n");
			TestGetCooperatorIdByIcsonId(ddwicsonId);
			break;
		}
        default:
            printf("Input Error:%s",argv[1]);
            break;
        }
    }
    else
    {
        printf("\nusage: cooperatorao_tester ServiceType \n\n");
        printf("No[1] TestGetCooperatorBaseInfo()     parm:CooperatorId/CooperatorSubAccount(uint64) \n");
        printf("No[2] TestGetCooperatorBaseInfoList() parm:CooperatorId/CooperatorSubAccount(uint64) \n");
        printf("No[3] TestUpdateCooperatorBaseInfo()  parm:dwCooperatorId cState strDiminutive strCooperatorAddrDiminutive strCustomerHotLine\n");
        printf("No[4] TestLogisticsSetCooperatorPro() parm:dwCooperatorId strLogisticsProIn strLogisticsProEx\n");
        printf("No[6] TestDelCooperatorCache()  parm:dwTmemBid dwCooperatorId \n");
        printf("No[7] TestGetCooperatorMonitorModelList()  parm:dwCooperatorId \n");
        printf("No[8] TestUpdateCooperatorMonitorModel()  parm:dwCooperatorId,dwModelId_u,dwModelId,strModelName_u,strModelName,dwModelState_u,dwModelState,dwMonitorTouch_u,dwMonitorTouch,dwModelType_u,dwModelType,vecModelRule_u,dwModelRuleSwitch \n");
        printf("No[9] TestDeleteCooperatorMonitorModel()  parm:dwCooperatorId dwModelId \n");
        printf("No[10] TestDelMonitorModelCache()  parm:dwTmemBid dwCooperatorId \n");
		printf("No[11] TestGetCooperatorIdByIcsonId()  parm: ddwIcsonIdId \n");
    }

    return 0;
}
